package com.griddroid.activities;

import com.griddroid.configuration.GridConfig;
import com.griddroid.nodes.GridWorker;
import com.griddroid.works.WorkRequest;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.ScrollView;

/**
 * An activity that runs on the worker node. It includes a {@link GridWorker
 * GridWorker} to process the received {@link WorkRequest WorkRequests}. The
 * activity itself just has a {@link LogTextBox LogTextBox} to output all the
 * debug messages.
 * 
 * @author Charalampos Mavroforakis
 * @author Thomas Ailianos
 * @author Michail Michailidis
 * 
 */
public class Worker extends Activity implements Runnable
{
	LogTextBox tv;
	ScrollView sv;
	GridWorker worker;

	/**
	 * Called when the activity is first created. It creates a separate thread
	 * for the GridWorker. It also sets a key listener on the Back button of the
	 * mobile device, so that the user can close the activity and the worker
	 * 
	 * @see GridWorker
	 * @see OnKeyListener
	 */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		tv = new LogTextBox(this);
		sv = new ScrollView(this);
		sv.addView(tv);
		setContentView(sv);
		Thread w_thread = new Thread(this);
		w_thread.start();
	}

	/**
	 * This is the handler responsible for any messages from the included grid
	 * worker
	 */
	public Handler messagehandler = new Handler()
	{
		/**
		 * It has been overridden, so that it will handle the custom messages
		 * accordingly. Through this method, the handler will print messages on
		 * the view (debug information)
		 * 
		 * @param msg
		 *            a message from the GridWorker. Its {@link Message#what
		 *            'what' attribute} is set according to the
		 *            {@link GridConfig GridConfig configuration}, denoting the
		 *            purpose of the message. The only message type that really
		 *            matters is the {@link GridConfig#UPDATE_TEXTVIEW
		 *            UPDATE_TEXTVIEW}
		 * 
		 */
		@Override
		public void handleMessage(Message msg)
		{
			if (msg.what == GridConfig.UPDATE_TEXTVIEW)
			{
				tv.append((String) msg.obj + "\n");
				sv.fullScroll(View.FOCUS_DOWN);
			}
			else if (msg.what == GridConfig.CLOSE)
			{
				worker=null;
				finish();
				
			}
		}
	};

	/**
	 * Initializes the GridWorker, sets its handler and finally starts the
	 * activity
	 */
	@Override
	public void run()
	{
		worker = new GridWorker(messagehandler);
		worker.start();
	}
	
	/**
	 * Overridden method to handle the press of the Back Button on the mobile
	 * device. It will shutdown the worker.
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent ev)
	{

		if (keyCode == KeyEvent.KEYCODE_BACK)
		{
			if (worker != null)
			{
				worker.sendEndSessionMessage();
				//worker = null;
			}
			return true;
		}
		return false;
	}

}